package com.happymmall.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.happymmall.service.UserService;
import com.happymmall.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user/forget_check_answer.do")
public class ForgetCheckAnswerServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        resp.setContentType("application/json;charset=utf-8");
        //获取参数
        String username = req.getParameter("username");
        String question = req.getParameter("question");
        String answer = req.getParameter("answer");

        //调用业务层
        String forgetToken = userService.forget_check_answer(username, question, answer);

        //处理结果
        if (forgetToken != null) {
            Map<String, Object> resultData = new HashMap();
            resultData.put("status", 0);
            resultData.put("data", forgetToken);

            JSONObject jsonObject = new JSONObject();
            String info = jsonObject.toJSONString(resultData);
            PrintWriter out = resp.getWriter();
            out.write(info);
        } else {
            Map<String, Object> resultData = new HashMap();
            resultData.put("status", 1);
            resultData.put("msg", "问题答案错误");

            JSONObject jsonObject = new JSONObject();
            String info = jsonObject.toJSONString(resultData);
            PrintWriter out = resp.getWriter();
            out.write(info);

        }


    }


}
